package com.example.hzzrpc.config.netty;

import com.example.hzzrpc.config.registrycenter.RpcServerContext;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author ：hzz
 * @description：TODO
 * @date ：2021/5/18 15:09
 */
@Slf4j
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private RpcServerContext rpcServerContext;

    public NettyServerHandler(RpcServerContext rpcServerContext) {
        this.rpcServerContext = rpcServerContext;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String channelId = ctx.channel().id().asLongText();
        String text = new String((byte[]) msg);
        System.out.println("接收到客户端"+channelId+",数据内容为:"+ text);
        Object o = rpcServerContext.doExecutionMethod("com.example.hzzrpcinterface.MyServiceServer", "hello");
        ctx.channel().writeAndFlush((text+";当前数据为:"+o).getBytes());
    }
}
